<template>
  <a-row>
<!--    <a-col :span="5">
      <model-tree @checked="getTreeData" @treeInit="getData" title="模型节点选择"></model-tree>
    </a-col>-->
    <a-col :span="24">
      <div ref="imageWrapper">
        <a-card title="查询条件" class="search">
          <!-- 查询区域 -->
          <div class="table-page-search-wrapper">
            <a-form-model ref="ruleForm" :model="queryParam" layout="inline"
                          @keyup.enter.native="searchQuery">
              <a-row :gutter="24">
<!--                <a-col :span="16">-->
                  <a-col :md="6" :sm="8">
                    <a-form-model-item label="采集表名称">
                      <a-input v-model="queryParam.name" placeholder="请输入采集表名称" />
                    </a-form-model-item>
                  </a-col>
                  <template v-if="toggleSearchStatus">
                    <a-col :md="6" :sm="8">
                      <a-form-item label="审计类型">
                        <j-dict-select-tag placeholder="请选择审计类型" v-model="queryParam.reportType"
                                           dictCode="report_type" />
                      </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                      <a-form-item label="报表类型">
                        <a-select
                          v-model="queryParam.taskCategoryId"
                          style="width: 100%"
                          placeholder="请选择报表类型"
                          option-label-prop="label"
                        >
                          <template v-for="(item, index) in taskList">
                            <a-select-option :value="item.id || ''" :label="item.name" :key="item.id">
                              {{ item.name }}
                            </a-select-option>
                          </template>
                        </a-select>
                      </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                      <a-form-model-item label="采集表版本">
                        <a-input addon-before="V" type="Number" :step="0.1" :min="1" v-model="queryParam.version"
                                 placeholder="请输入采集表版本" />
                      </a-form-model-item>
                    </a-col>
                    <a-col :md=18 :sm="24">
                      <a-form-item label="发布日期">
                        <j-date placeholder="请选择开始日期" class="query-group-cust"
                                v-model="queryParam.addTime_begin"></j-date>
                        <span class="query-group-split-cust"></span>
                        <j-date placeholder="请选择结束日期" class="query-group-cust"
                                v-model="queryParam.addTime_end"></j-date>
                      </a-form-item>
                    </a-col>
                  </template>
<!--                </a-col>-->
                  <a-col :md="18" :sm="24">
                    <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
                      <a-button type="primary" icon="search" class="yellow_search_btn" @click="searchQuery">查询</a-button>
                      <a-button icon="reload" class="clear_btn" @click="searchReset">清除条件</a-button>
                      <a-button icon="download" class="clear_btn" @click="batchDistribution">批量下发</a-button>
                      <a-button icon="download" class="clear_btn" @click="batchExportAllTemplate">批量导出模板</a-button>
                      <a @click="handleToggleSearch">
                        <a-button class="clear_btn"> 高级搜索  <a-icon
                          :type="toggleSearchStatus ? 'up' : 'down'" /></a-button>
                      </a>
                    </span>
                  </a-col>
              </a-row>
            </a-form-model>
          </div>
        </a-card>
        <a-card style="margin-top: 20px">

          <!--      <template slot="title"><span style="font-weight: bold">模板列表</span></template>-->
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            class="j-table-force-nowrap"
            :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            @change="handleTableChange">
            <template slot="action" slot-scope="text, record">
              <a-button @click="OnPerview(record)" style="margin-left:5px">预览</a-button>
              <a-button @click="OnDown(record)" style="margin-left:5px">下发</a-button>
            </template>
          </a-table>
          <PerviewModal ref="perviewModal"></PerviewModal>
          <DownModal ref="DownModal"></DownModal>
          <!--      <CheckOrganModal ref="departModal" @ok="updateQueryParam"></CheckOrganModal>-->
        </a-card>
      </div>
    </a-col>
  </a-row>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

import columns from './columns'
import PerviewModal from '../perview/PerviewModal'
import DownModal from './down/DownModal'
import { getAction, postAction } from '@api/manage'
import ModelTree from '@views/sj/sjcj/rwgl/rwxf/modelTree/index.vue'

export default {
  components: {
    ModelTree,
    // CheckOrganModal,
    PerviewModal,
    DownModal
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      queryParam: {},
      superParam: {},
      url: {
        list: '/sjtb/dataCollectionTemplateFile/list',
        exportAllTemplateByModelId: '/sjmx2/modelTemplateConfig/exportAllTemplateByModelId',
      },
      columns: columns(),
      multiSelectAcrossPagesRowKey: 'id',
      taskList: [],
      modelId:'',
      searchModelId:'',
      disableMixinCreated: false,
    }
  },
  computed: {},
  created() {
    this.getTaskList()
  },
  watch: {},
  methods: {
    // updateQueryParam(data) {
    //   console.log(data);
    //   debugger
    // },
    getData(value){
      this.modelId = value.join(',')
      this.queryParam.modelId = this.modelId
      this.searchModelId = this.modelId
      // this.loadData()
    },

    getTaskList() {
      getAction('/sjtb/dataCollectionTaskCategory/list').then((res) => {
        if (res.success && res.result.length > 0) {
          this.taskList = res.result
        } else {
          console.log(res)
        }
      })
    },
    OnDown(record) {
      // this.$refs.departModal.show()
      this.$refs.DownModal.edit(record)
      this.$refs.DownModal.title = '请选择下发对象'
      this.$refs.DownModal.disableSubmit = false
    },
    OnPerview(record) {
      this.$refs.perviewModal.edit(record)
      this.$refs.perviewModal.title = `${record.name}（v ${record.version}）`
      this.$refs.perviewModal.disableSubmit = false
    },

    getTreeData(value){
      this.modelId = value.join(',')
    },

    batchExportAllTemplate() {
      if (this.selectedRowKeys.length == 0 && !this.modelId) {
        this.$message.error('请选择模板')
        return
      }
      let id = this.selectionRows.map((item) => item.id).join(',')
      getAction(this.url.exportAllTemplateByModelId,
        {
          ids: this.modelId,
          templateIds: id
        }
      ).then((res) => {
        if (res.success) {
          let url = `${window._CONFIG['domianURL']}/sys/common/download` + res.result
          window.open(url, '_target')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    
    batchDistribution() {
      console.log(this.selectedRowKeys)
      console.log(this.selectionRows)
      if (this.selectedRowKeys.length == 0) {
        this.$message.error('请选择模板')
        return
      }
      let id = this.selectionRows.map((item) => item.id).join(',')
      let versionId = this.selectionRows.map((item) => item.versionId).join(',')
      this.$refs.DownModal.edit({
        id,
        versionId
      }, false)
      this.$refs.DownModal.title = '请选择下发对象'
      this.$refs.DownModal.disableSubmit = false
    },

    searchReset() {
      this.queryParam = {}
      this.queryParam.modelId = this.modelId
      this.searchModelId = this.modelId
      this.loadData(1)
    },

    searchQuery() {
      this.searchModelId = this.modelId
      this.queryParam.modelId = this.modelId
      this.loadData(1)
      if (typeof this.getTop === 'function') {
        this.getTop()
      }
    },
  }
}
</script>
<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';

.search {
  background: url(/img/search_bg.204329a9.png) 100% 100%;
}

</style>
